﻿@using PagedList
@using PagedList.Mvc

@model IPagedList<ExcellOnServices.Models.Product>

@{
    ViewBag.Title = "List Products";
}
<div class="panel panel-primary">
    <div class="panel-heading">Products List</div>
    <div class="panel-body">
        @using (Html.BeginForm("Index", "Products", new { sortBy = ViewBag.SortBy, sortAsce = ViewBag.SortOrder }, FormMethod.Post, new { @class = "form-inline" }))
        {
            <div class="form-group">
                @Html.DropDownList("category", (IEnumerable<SelectListItem>)ViewBag.Category, "--Select Category--", new { @class = "form-control" })
            </div>
            <div class="pull-right">
                <div class="form-group">
                    @Html.TextBox("search", null, new { @class = "form-control", @placeholder = "Product Name" })
                </div>
                <button type="submit" class="btn btn-primary">Search</button>
                <input type="reset" value="Clear" class="btn btn-default" />
            </div>
        }
    </div>
    @if (!Model.Any())
    {
        <div class="alert alert-info">
            No record were found.
            @Html.ActionLink("Back to show all products", "Index", "Products", null, new { @class = "alert-link" })
        </div>
    }
    else
    {
        <table class="table table-striped table-hover">
            <tr>
                <th>
                    Image
                </th>
                <th>
                    @Html.ActionLink("Category", "Index", new { sortBy = "category", sortAsce = !ViewBag.SortOrder, category = ViewBag.CategoryId, search = ViewBag.SearchString, page = ViewBag.Page })
                </th>
                <th>
                    @Html.ActionLink("Name", "Index", new { sortBy = "name", sortAsce = !ViewBag.SortOrder, category = ViewBag.CategoryId, search = ViewBag.SearchString, page = ViewBag.Page })
                </th>
                <th>
                    @Html.ActionLink("Price", "Index", new { sortBy = "price", sortAsce = !ViewBag.SortOrder, category = ViewBag.CategoryId, search = ViewBag.SearchString, page = ViewBag.Page })
                </th>
                <th>
                    @Html.ActionLink("Discount", "Index", new { sortBy = "discount", sortAsce = !ViewBag.SortOrder, category = ViewBag.CategoryId, search = ViewBag.SearchString, page = ViewBag.Page })
                </th>
                <th>
                    Edit
                </th>
                <th>
                    Delete
                </th>
                <th>
                    Details
                </th>
                <th></th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        <img src="@item.ImageUrl" style="width:50px;" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Category.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Price)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Discount)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-primary btn-sm" })
                    </td>
                    <td>
                        @if (!item.OrderItems.Any())
                        {
                            @Html.ActionLink("Delete", "Delete", new { id = item.Id, search = ViewBag.SearchString, sortBy = ViewBag.SortBy, sortAsce = ViewBag.SortOrder, category = ViewBag.CategoryId, page = ViewBag.Page }, new { @class = "btn btn-danger btn-sm", onclick = "return confirm('Are you sure you wish to delete this product?');" })
                        }
                    </td>
                    <td>
                        @Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-info btn-sm" })
                    </td>
                </tr>
            }
        </table>
        <div class="panel-footer">
            <center>@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortBy = ViewBag.SortBy, sortAsce = ViewBag.SortOrder, search = ViewBag.SearchString, category = ViewBag.CategoryId}))</center>
        </div>
    }
</div>
